<?php declare(strict_types=1);


namespace App\Http\Controller;

use App\Model\Entity\Article;
use App\Model\Entity\Count;
use App\Model\Entity\PostsModel;
use App\Model\Entity\User;
use App\Model\Logic\ElasticSearchLogic;
use Elasticsearch\ClientBuilder;
use Exception;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Db\DB;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Redis\Pool;
use Swoftx\Elasticsearch\CoroutineHandler;
use Throwable;

/**
 * Class DbModelController
 *
 * @since 2.0
 *
 * @Controller("db")
 */
class DbModelController extends BaseController
{

    /**
     * @Inject("redis.pool")
     * @var Pool
     */
    private $redis;

    /**
     * @RequestMapping(route="find")
     * @throws Throwable
     */
    public function find()
    {
        $members = $this->redis->hGetAll('member:member.987');
        return $this->response(10000, $members);
    }

    /**
     * @RequestMapping(route="save")
     *
     * @return array
     *
     * @throws Exception
     */
    public function save(): array
    {
        $user = new User();
        $user->setAge(mt_rand(1, 100));
        $user->setUserDesc('desc');

        $user->save();

        $count = Count::new();
        $count->setUserId($user->getId());
        $count->save();

        return $user->toArray();
    }

    /**
     * @RequestMapping(route="update")
     *
     * @return array
     *
     * @throws Throwable
     */
    public function update(): array
    {
        $id = $this->getId();

        User::updateOrInsert(['id' => $id], ['name' => 'swoft']);

        $user = User::find($id);

        return $user->toArray();
    }

    /**
     * @RequestMapping(route="delete")
     *
     * @return array
     *
     * @throws Throwable
     */
    public function delete(): array
    {
        $id     = $this->getId();
        $result = User::find($id)->delete();

        return [$result];
    }

    /**
     * @return int
     * @throws Throwable
     */
    public function getId()
    {
        $attr = [
            'title' => 'title demo',
            'create_time' => time()
        ];
        $id = PostsModel::insertGetId($attr);
        return $id;
    }
//
//    /**
//     * @RequestMapping()
//     *
//     * @return array
//     * @throws Throwable
//     */
//    public function batchUpdate()
//    {
//        // User::truncate();
//        User::updateOrCreate(['id' => 1], ['age' => 23]);
//        User::updateOrCreate(['id' => 2], ['age' => 23]);
//
//        $values = [
//            ['id' => 1, 'age' => 18],
//            ['id' => 2, 'age' => 19],
//        ];
//        $values = array_column($values, null, 'id');
//
//        User::batchUpdateByIds($values);
//
//        $users = User::find(array_column($values, 'id'));
//
//        $updateResults = [];
//        /* @var User $user */
//        foreach ($users as $user) {
//            $updateResults[$user->getId()] = true;
//            if ($user->getAge() != $values[$user->getId()]['age']) {
//                $updateResults[$user->getId()] = false;
//            }
//        }
//
//
//        return $updateResults;
//    }
}
